//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.oms.esb.provider.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
    public static final String HK_DATE_FORMAT = "dd/MMM/yyyy HH:mm:ss";
    public static final String HK_DATE_FORMAT_2 = "dd/MM/yyyy HH:mm";
    public static final String HK_DATE_FORMAT_NO_DELIMITER = "dd/MMM/yyyy";
    public static final String HK_DATE_FORMAT_1 = "dd-MMM-yyyy HH:mm:ss";
    public static final String HK_DATE_FORMAT_NO_SECOND_1 = "dd-MMM-yyyy HH:mm";
    public static final String HK_DATE_FORMAT_NO_DELIMITER_1 = "dd-MMM-yyyy";
    public static final String CN_DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";
    public static final String CN_DATE_FORMAT_NO_DELIMITER = "yyyy/MM/dd";
    public static final String CN_DATE_FORMAT_1 = "yyyy-MM-dd HH:mm:ss";
    public static final String CN_DATE_FORMAT_2 = "yyyy-MM-dd HH:mm";
    public static final String CN_DATE_FORMAT_NO_DELIMITER_1 = "yyyy-MM-dd";
    public static final String CN_DATE_FORMAT_3 = "yyyyMMdd";
    public static final String CN_DATE_FORMAT_4 = "yyyyMMddHHmmss";
    public static final String CN_DATE_FORMAT_5 = "HH:mm";

    public DateUtil() {
    }

//    public static void main(String[] args) {
//        System.out.println(format(new Date(), "dd-MMM-yyyy HH:mm", Locale.ENGLISH));
//    }

    public static Date parse(String str, String pattern) {
        if (StringUtils.isEmpty(str)) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);

            try {
                return sdf.parse(str);
            } catch (Exception var4) {
                return null;
            }
        }
    }

    public static Date parse(String str, String pattern, Locale locale) {
        if (StringUtils.isEmpty(str)) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);

            try {
                return sdf.parse(str);
            } catch (Exception var5) {
                return null;
            }
        }
    }

    public static String format(Date date, String pattern) {
        if (Objects.isNull(date)) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);

            try {
                return sdf.format(date);
            } catch (Exception var4) {
                return "";
            }
        }
    }

    public static String format(Date date, String pattern, Locale locale) {
        if (Objects.isNull(date)) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);

            try {
                return sdf.format(date);
            } catch (Exception var5) {
                return "";
            }
        }
    }

    public static String format2UpperCase(Date date, String pattern, Locale locale) {
        if (Objects.isNull(date)) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);

            try {
                return sdf.format(date).toUpperCase();
            } catch (Exception var5) {
                return "";
            }
        }
    }

    public static int getIntervalDay(Date startDate, Date endDate) {
        if (Objects.isNull(startDate)) {
            return 0;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            try {
                startDate = sdf.parse(sdf.format(startDate));
                endDate = sdf.parse(sdf.format(endDate));
            } catch (ParseException var10) {
                var10.printStackTrace();
            }

            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(endDate);
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / 86400000L;
            return Integer.parseInt(String.valueOf(between_days));
        }
    }

    public static List<Date> getDaysBetween(String startDate, String endDate) {
        List<Date> days = new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

        try {
            Date start = dateFormat.parse(startDate);
            Date end = dateFormat.parse(endDate);
            days = getDaysBetween(start, end);
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        return (List)days;
    }

    public static List<Date> getDaysBetween(Date startDate, Date endDate) {
        List<Date> days = new ArrayList();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(startDate);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(endDate);
        tempEnd.add(5, 1);

        while(tempStart.before(tempEnd)) {
            days.add(tempStart.getTime());
            tempStart.add(6, 1);
        }

        return days;
    }

    public static String countAgeByBirth(Date birth) {
        String age = null;

        try {
            Calendar now = Calendar.getInstance();
            now.setTime(new Date());
            Calendar birthday = Calendar.getInstance();
            birthday.setTime(birth);
            if (birthday.after(now)) {
                age = "0";
            } else {
                int num = now.get(1) - birthday.get(1);
                if (now.get(6) >= birthday.get(6)) {
                    ++num;
                }

                age = String.valueOf(num);
            }

            return age;
        } catch (Exception var5) {
            return null;
        }
    }

    public static boolean isTimeIn(Date time, Date begin, Date end) {
        return time.getTime() >= begin.getTime() && time.getTime() <= end.getTime();
    }

    public static boolean isToday(Date date) {
        Date now = new Date();
        String nowStr = format(now, "yyyy-MM-dd");
        String dateStr = format(date, "yyyy-MM-dd");
        return dateStr.equals(nowStr);
    }

    public static Date additionTime(Date date, int num, String type) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if ("1".equals(type)) {
            c.add(5, num);
        } else if ("2".equals(type)) {
            c.add(2, num);
        } else if ("3".equals(type)) {
            c.add(1, num);
        }

        return c.getTime();
    }

    public static List<String> queryAllDay(int year, int month) {
        SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
        List<String> fullDayList = new ArrayList(32);
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(1, year);
        cal.set(2, month - 1);
        cal.set(5, 1);
        int count = cal.getActualMaximum(5);

        for(int j = 1; j <= count; ++j) {
            fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
            cal.add(5, 1);
        }

        return fullDayList;
    }

    public static String queryDate(Date date) {
        String resultDate = null;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            System.out.println("要计算日期为:" + sdf.format(cal.getTime()));
            int dayWeek = cal.get(7);
            if (1 == dayWeek) {
                cal.add(5, -1);
            }

            cal.setFirstDayOfWeek(2);
            int day = cal.get(7);
            cal.add(5, cal.getFirstDayOfWeek() - day);
            System.out.println("所在周星期一的日期：" + sdf.format(cal.getTime()));
            cal.add(5, 6);
            resultDate = sdf.format(cal.getTime());
            System.out.println("所在周星期日的日期：" + sdf.format(cal.getTime()));
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return resultDate;
    }

    public static Date getFirstDayOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(1);
        cal.clear();
        cal.set(1, year);
        Date firstDayOfYear = cal.getTime();
        return firstDayOfYear;
    }

    public static Date getLastDayOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(1);
        cal.clear();
        cal.set(1, year);
        cal.roll(6, -1);
        Date lastDayOfYear = cal.getTime();
        return lastDayOfYear;
    }
}
